package com.coder4j.starter.mybatis;

import com.coder4j.starter.web.BaseRequest;
import lombok.Data;

import java.util.List;

/**
 * 基础的页面
 * @author Keller
 * @date 2021-02-09 00:00:01
 */
@Data
public class BasePage<T> {
    /**
     * 页面大小
     */
    private Integer pageSize;

    /**
     * 页码
     */
    private Integer pageNo;

    private Integer pageNum;

    /**
     * 数据总数
     */
    private Integer count;

    private Integer total;

    /**
     * 总页数
     */
    private Integer pageCount;

    /**
     * 数据列表
     */
    private List<T> dataList;

    /**
     * 自定义数据
     * 可以根据业务需要在列表数据外补充自定义数据
     */
    private Object customData;

    /**
     * 构造分页结果
     * @param query
     * @param count
     */
    public void initByQuery(BaseQuery query,Integer count){
        this.setPageSize(query.getPageSize());
        this.setPageNo(query.getPageNo());
        this.setPageNum(query.getPageNo());

        this.setCount(count);
        this.setTotal(count);
        this.setPageCount(count / this.getPageSize() + (count % this.getPageSize() > 0 ? 1 : 0));
    }

    public void initByQuery(BaseRequest query, Integer count){
        this.setPageSize(query.getPageSize());
        this.setPageNo(query.getPageNo());
        this.setPageNum(query.getPageNo());

        this.setCount(count);
        this.setTotal(count);
        this.setPageCount(count / this.getPageSize() + (count % this.getPageSize() > 0 ? 1 : 0));
    }
}
